#ifndef THINK_THINK_LOG_H
#define THINK_THINK_LOG_H

static char* think_log =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think;\n"
	"\n"
	"use think\\exception\\ClassNotFoundException;\n"
	"\n"
	"/**\n"
	" * Class Log\n"
	" * @package think\n"
	" *\n"
	" * @method void log($msg) static 记录一般日志\n"
	" * @method void error($msg) static 记录错误日志\n"
	" * @method void info($msg) static 记录一般信息日志\n"
	" * @method void sql($msg) static 记录 SQL 查询日志\n"
	" * @method void notice($msg) static 记录提示日志\n"
	" * @method void alert($msg) static 记录报警日志\n"
	" */\n"
	"class Log\n"
	"{\n"
	"    const LOG    = \'log\';\n"
	"    const ERROR  = \'error\';\n"
	"    const INFO   = \'info\';\n"
	"    const SQL    = \'sql\';\n"
	"    const NOTICE = \'notice\';\n"
	"    const ALERT  = \'alert\';\n"
	"    const DEBUG  = \'debug\';\n"
	"\n"
	"    /**\n"
	"     * @var array 日志信息\n"
	"     */\n"
	"    protected static $log = [];\n"
	"\n"
	"    /**\n"
	"     * @var array 配置参数\n"
	"     */\n"
	"    protected static $config = [];\n"
	"\n"
	"    /**\n"
	"     * @var array 日志类型\n"
	"     */\n"
	"    protected static $type = [\'log\', \'error\', \'info\', \'sql\', \'notice\', \'alert\', \'debug\'];\n"
	"\n"
	"    /**\n"
	"     * @var log\\driver\\File|log\\driver\\Test|log\\driver\\Socket 日志写入驱动\n"
	"     */\n"
	"    protected static $driver;\n"
	"\n"
	"    /**\n"
	"     * @var string 当前日志授权 key\n"
	"     */\n"
	"    protected static $key;\n"
	"\n"
	"    /**\n"
	"     * 日志初始化\n"
	"     * @access public\n"
	"     * @param  array $config 配置参数\n"
	"     * @return void\n"
	"     */\n"
	"    public static function init($config = [])\n"
	"    {\n"
	"        $type  = isset($config[\'type\']) ? $config[\'type\'] : \'File\';\n"
	"        $class = false !== strpos($type, \'\\\\\') ? $type : \'\\\\think\\\\log\\\\driver\\\\\' . ucwords($type);\n"
	"\n"
	"        self::$config = $config;\n"
	"        unset($config[\'type\']);\n"
	"\n"
	"        if (class_exists($class)) {\n"
	"            self::$driver = new $class($config);\n"
	"        } else {\n"
	"            throw new ClassNotFoundException(\'class not exists:\' . $class, $class);\n"
	"        }\n"
	"\n"
	"        // 记录初始化信息\n"
	"        App::$debug && Log::record(\'[ LOG ] INIT \' . $type, \'info\');\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 获取日志信息\n"
	"     * @access public\n"
	"     * @param  string $type 信息类型\n"
	"     * @return array|string\n"
	"     */\n"
	"    public static function getLog($type = \'\')\n"
	"    {\n"
	"        return $type ? self::$log[$type] : self::$log;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 记录调试信息\n"
	"     * @access public\n"
	"     * @param  mixed  $msg  调试信息\n"
	"     * @param  string $type 信息类型\n"
	"     * @return void\n"
	"     */\n"
	"    public static function record($msg, $type = \'log\')\n"
	"    {\n"
	"        self::$log[$type][] = $msg;\n"
	"\n"
	"        // 命令行下面日志写入改进\n"
	"        IS_CLI && self::save();\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 清空日志信息\n"
	"     * @access public\n"
	"     * @return void\n"
	"     */\n"
	"    public static function clear()\n"
	"    {\n"
	"        self::$log = [];\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 设置当前日志记录的授权 key\n"
	"     * @access public\n"
	"     * @param  string $key 授权 key\n"
	"     * @return void\n"
	"     */\n"
	"    public static function key($key)\n"
	"    {\n"
	"        self::$key = $key;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 检查日志写入权限\n"
	"     * @access public\n"
	"     * @param  array $config 当前日志配置参数\n"
	"     * @return bool\n"
	"     */\n"
	"    public static function check($config)\n"
	"    {\n"
	"        return !self::$key || empty($config[\'allow_key\']) || in_array(self::$key, $config[\'allow_key\']);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 保存调试信息\n"
	"     * @access public\n"
	"     * @return bool\n"
	"     */\n"
	"    public static function save()\n"
	"    {\n"
	"        // 没有需要保存的记录则直接返回\n"
	"        if (empty(self::$log)) {\n"
	"            return true;\n"
	"        }\n"
	"\n"
	"        is_null(self::$driver) && self::init(Config::get(\'log\'));\n"
	"\n"
	"        // 检测日志写入权限\n"
	"        if (!self::check(self::$config)) {\n"
	"            return false;\n"
	"        }\n"
	"\n"
	"        if (empty(self::$config[\'level\'])) {\n"
	"            // 获取全部日志\n"
	"            $log = self::$log;\n"
	"            if (!App::$debug && isset($log[\'debug\'])) {\n"
	"                unset($log[\'debug\']);\n"
	"            }\n"
	"        } else {\n"
	"            // 记录允许级别\n"
	"            $log = [];\n"
	"            foreach (self::$config[\'level\'] as $level) {\n"
	"                if (isset(self::$log[$level])) {\n"
	"                    $log[$level] = self::$log[$level];\n"
	"                }\n"
	"            }\n"
	"        }\n"
	"\n"
	"        if ($result = self::$driver->save($log, true)) {\n"
	"            self::$log = [];\n"
	"        }\n"
	"\n"
	"        Hook::listen(\'log_write_done\', $log);\n"
	"\n"
	"        return $result;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 实时写入日志信息 并支持行为\n"
	"     * @access public\n"
	"     * @param  mixed  $msg   调试信息\n"
	"     * @param  string $type  信息类型\n"
	"     * @param  bool   $force 是否强制写入\n"
	"     * @return bool\n"
	"     */\n"
	"    public static function write($msg, $type = \'log\', $force = false)\n"
	"    {\n"
	"        $log = self::$log;\n"
	"\n"
	"        // 如果不是强制写入，而且信息类型不在可记录的类别中则直接返回 false 不做记录\n"
	"        if (true !== $force && !empty(self::$config[\'level\']) && !in_array($type, self::$config[\'level\'])) {\n"
	"            return false;\n"
	"        }\n"
	"\n"
	"        // 封装日志信息\n"
	"        $log[$type][] = $msg;\n"
	"\n"
	"        // 监听 log_write\n"
	"        Hook::listen(\'log_write\', $log);\n"
	"\n"
	"        is_null(self::$driver) && self::init(Config::get(\'log\'));\n"
	"\n"
	"        // 写入日志\n"
	"        if ($result = self::$driver->save($log, false)) {\n"
	"            self::$log = [];\n"
	"        }\n"
	"\n"
	"        return $result;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 静态方法调用\n"
	"     * @access public\n"
	"     * @param  string $method 调用方法\n"
	"     * @param  mixed  $args   参数\n"
	"     * @return void\n"
	"     */\n"
	"    public static function __callStatic($method, $args)\n"
	"    {\n"
	"        if (in_array($method, self::$type)) {\n"
	"            array_push($args, $method);\n"
	"\n"
	"            call_user_func_array(\'\\\\think\\\\Log::record\', $args);\n"
	"        }\n"
	"    }\n"
	"\n"
	"}\n"
	"\n"
;
#endif